package com.financing.platform.task;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.platform.SMSDict;
import com.financing.platform.SMSSend;
import com.financing.platform.dao.IMessageSmsDao;
import com.financing.platform.dao.ISwitchDao;
import com.financing.platform.dto.MessageSmsDTO;
import com.financing.platform.dto.SwitchDTO;
import com.financing.platform.util.Dict;

/**
 * 
* @ClassName: SmsSendThread 
* @Description: 系统短信下发  
* @author chenming 
* @date 2014年9月22日 下午6:32:23 
*
 */
@Component(value="smsSendJob" )
public class SmsSendJob {
	
	Logger log = Logger.getLogger(this.getClass());
	
	private static boolean runTimeFlag = false;
	
	@Autowired
	private IMessageSmsDao messageSmsDaoImpl;
	
	@Autowired
	private ISwitchDao switchDaoImpl;
	
	@Scheduled(cron = "0 0/2 * * * ?") 
	public void sendMessageJob(){
		if(runTimeFlag){
			return ;
		}else{
			runTimeFlag = true;
		}
		MessageSmsDTO messageSmsDTO  = new MessageSmsDTO();
		log.info("Messageproducer begin run");
						
		messageSmsDTO.setFpSmsSendTime(null);
		messageSmsDTO.setFpSmsSendType(Dict.message_type_now);
		messageSmsDTO.setFpSmsSendStatus(Dict.message_status_not_send);
		messageSmsDTO.setFpSmsSendTimes(Dict.message_max_send_times);
		messageSmsDTO.setBegin(0);
		messageSmsDTO.setEnd(200);
		messageSmsDTO.setUpdateBy("system");
		messageSmsDTO.setUpdateDate(new Date());
		//实时发送的短信
		for(int i = 0;i<=3;i++){
			List<MessageSmsDTO> messageList = messageSmsDaoImpl.messageSmsList(messageSmsDTO);
			for(MessageSmsDTO message:messageList){
				try {
					messageSmsDaoImpl.updateMessageSmsStatus(message);
					messageSend(message);
				} catch (SQLException e) {
					log.warn("update Message status error:",e);
				}
			}
		}
							
		//定时发送的短信
		messageSmsDTO.setFpSmsSendType(Dict.message_type_after);
		messageSmsDTO.setFpSmsSendTime(new Date());
		List<MessageSmsDTO> messageList = messageSmsDaoImpl.messageSmsList(messageSmsDTO);
		for(MessageSmsDTO message:messageList){
				message.setFpSmsSendStatus(Dict.message_send_status_sending);
				try {
					messageSmsDaoImpl.updateMessageSmsStatus(message);
					messageSend(message);
				} catch (SQLException e) {
					log.warn("update Message status error:",e);
				}
		}
		runTimeFlag = false;
}

	/**
	 * 
	* @Title: messageSend 
	* @Description:  发送短信息
	* @param   message    
	* @return void   
	* @throws
	 */
	private void messageSend(MessageSmsDTO message) {
				SMSSend smsSend = new SMSSend();
				List<String>  mobileList = new ArrayList<String>();
				mobileList.add(message.getFpSmsRecivePhoneNo());
				Map<String,Object> result = new HashMap<String,Object>();
				try {
					//判断短信开关是否打开，打开则发送，不打开则永久不发送
					SwitchDTO switchs = new SwitchDTO();
					switchs.setsKey(message.getFpSmsType());
					switchs = switchDaoImpl.querySwitch(switchs);
					
					if(Dict.switchOn.equals(switchs.getsValue())){
						result = smsSend.sendSMS(mobileList, message.getFpSmsContent());
					}else{
						log.info("switch is off ");
						result.put(SMSDict.result, Dict.sms_send_success);
					}
					
					if(Dict.sms_send_success.equals(result.get(SMSDict.result))){
						message.setFpSmsSendStatus(Dict.message_send_status_success);
						message.setFpSmsSendTime(new Date());
					}else{
						message.setFpSmsSendStatus(Dict.message_send_status_fail);
						message.setFpSmsSendTime(new Date());
					}
				} catch (Exception e) {
					log.error("send sms error!",e);
					message.setFpSmsSendTime(new Date());
					message.setFpSmsSendStatus(Dict.message_send_status_fail);
				}
				try {
					message.setFpSmsSendTimes(message.getFpSmsSendTimes()+1);
					message.setFpSmsSendTime(new Date());
					messageSmsDaoImpl.updateMessageSmsStatus(message);
				} catch (SQLException e) {
					log.warn("update message error!",e);
				}
			}
			
	}
